package junitTests;

import static org.junit.Assert.*;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;

import upload.UploadAudio;

import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class UploadMediaTest {

	private final LocalServiceTestHelper helper = 
			new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
	
	@Before
	public void prepare() {
		helper.setUp();
	}
	/*
	 * 
	 */
	@Test (expected = NullPointerException.class)
	public void UploadOneNewFileToDatastore() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		Query query = new Query("Audio", audioFileKey);
        assertEquals(0, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
        BlobKey blobKey = new BlobKey("ABC123");
        //BlobInfo blobInfo = new BlobInfo(blobKey, "", new Date(), "", 0);
        HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "testfile", "audio", blobKey);
        assertEquals(1, ds.prepare(query).countEntities(FetchOptions.Builder.withLimit(5)));
	}
	/*
	 * 
	 */
	@Test (expected = NullPointerException.class)
	public void CorrectFileNameStored() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		Query query = new Query("Audio", audioFileKey);
        BlobKey blobKey = new BlobKey("ABC123");
        HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "username", "mediaName", blobKey);
        assertEquals("mediaName", ds.prepare(query).asSingleEntity().getProperty("mediaName"));
	}
	/*
	 * 
	 */
	@Test (expected = NullPointerException.class)
	public void CorrectFileTypeStored() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		Query query = new Query("Audio", audioFileKey);
        BlobKey blobKey = new BlobKey("ABC123");
        HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "username", "mediaName", blobKey);
        assertEquals("username", ds.prepare(query).asSingleEntity().getProperty("user"));
	}
	/*
	 * 
	 */
	@Test (expected = NullPointerException.class)
	public void CorrectBlobKeyStored() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key audioFileKey = KeyFactory.createKey("AudioFiles", "mediaFiles");
		Query query = new Query("Audio", audioFileKey);
        BlobKey blobKey = new BlobKey("ABC123");
        HttpServletRequest request = null;
		HttpServletResponse response = null;
		Entity file = new Entity("Audio", audioFileKey);
        UploadAudio.AddEntityInfo(request, response, file, "username", "mediaName", blobKey);
        assertEquals(blobKey, ds.prepare(query).asSingleEntity().getProperty("blobKey"));
	}
}
